package com.moleq.dialog;

import java.util.Calendar;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.mgpos.activities.R;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.mgpos.widget.NumberPadForPayment;
import com.moleq.mgpos.widget.NumberPadForPayment.NumberPadListener;
import com.moleq.util.MoleQUtil;

public class InputDateInfoDialog extends MoleQBaseDialog {

	private Button btnConfirm;
	private Button btnClose;
	private NumberPadForPayment mNumPad;
	private String age_time;
	private int viewid;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btn_time_close:
				dismiss();
				break;
			case R.id.btn_time_confirm:
				String strAuth = mNumPad.getBuffer();
				if(strAuth == null || strAuth.equals("")) return;
				if (strAuth != null && !strAuth.equals("")) {
					age_time = changeDate(strAuth);
				}
				
				if(!age_time.equals(""))onCloseWithReturn(viewid,age_time);

				break;
			}
		}

	};
	
	private NumberPadListener mNumPadListener = new NumberPadListener(){

		@Override
		public void onClick(NumberPadButton button) {
			String time = mNumPad.getBuffer();
			switch(button){
			case ENTER:
//				String strAuth = mNumPad.getBuffer();
//				if(strAuth!=null && !strAuth.equals("")){
//					onCloseWithReturn(strAuth);
//				}
				break;
			case DOT:
				time = time.replace(".", "");
				mNumPad.setBuffer(time);
				break;
			}
			
		}
		
	};
	private TextView tv_title;

	
	public String changeDate(String time){
		String age = "";
	    boolean bo = true;
	    if(time.length() != 8) bo = false;
	    
		age = MoleQUtil.fixDateTime2(time, "yyyy-MM-dd", "MMddyyyy");
		 if(age.equals("")) bo = false;
		if(!bo){
			age= "";
			MessageBox.show(mContext, "The date is not vaild.");
		}
		return age;
	}


	public InputDateInfoDialog(Context context, Object base) {
		super(context,R.style.dialog,base);
	}
	
	public InputDateInfoDialog(Context context, int theme,Object base) {
		super(context, theme,base);
	}
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_input_time_info);
		this.setCancelable(false);

		btnConfirm = (Button) findViewById(R.id.btn_time_confirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.btn_time_close);
		btnClose.setOnClickListener(mOnClickListener);

		mNumPad = (NumberPadForPayment) findViewById(R.id.authNumPad);
		mNumPad.setNumberPadListener(mNumPadListener);

		tv_title = (TextView)findViewById(R.id.tv_title);
	}
	
	public void show(int vid,String dateText) {
		// TODO Auto-generated method stub
		super.show();
		
		Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int monthOfYear=calendar.get(Calendar.MONTH)+1;
        int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        tv_title.setText("Date(MMddYYYY)");
        
		viewid = vid;
		mNumPad.reset();
		mNumPad.requestFocusFromTouch();
		if (dateText.equals("")) {
			mNumPad.setBuffer(String.format("%02d", monthOfYear)+String.format("%02d",dayOfMonth)+String.valueOf(year));
		}else{
			mNumPad.setBuffer( MoleQUtil.fixDateTime2(dateText,"MMddyyyy","yyyy-MM-dd"));
		}
		
	}
	
}
